import subprocess
import time


class AaltoASRInterface(object):
    """
    This module is used to interface with the AaltoASR recognition library
    through terminal commands.
    """

    def __init__(self, exe_path="./decode-stream-wav"):
        """
        This is given a path to the location of the C++ executable that in turn
        uses the necessary AaltoASR functions.
        """
        temp = ""

        self.proc = subprocess.Popen(exe_path,
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)

        #Wait for init done
        while "INIT DONE" not in temp:
            temp = self.proc.stdout.readline() 
    
    def analyzeFile(self, file_path):
        """
        This analyzes the given audio file and returns the results of the
        analysis.
        """
        
        
        temp = ""
        returnThis = ""
        self.proc.stdin.write(file_path + "\n")
        while not "CONFIDENCE" in temp:
            temp = self.proc.stdout.readline()
            returnThis +=temp
        print temp
            
        return returnThis
            
    def closeInterface(self):
        """
        This tells the C++ executable to end.
        """
        self.proc.stdin.write("quit")
